<template>
  <el-dialog title="新增人员" :visible="isShow" width="630px" @close="closeFn">
    <el-form ref="addForm" :rules="rules" :model="addForm">
      <el-form-item label="人员名称：" prop="userName" label-width="150px">
         <el-input
            style="width: 85%"
            v-model="addForm.userName"
            placeholder="请输入"
            maxlength="11"
            show-word-limit
            autocomplete="off"
          ></el-input>
      </el-form-item>
      <el-form-item label="角色:"  label-width="150px" prop="roleId">
       <el-select
          style="width: 85%"
          v-model="addForm.roleId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in userList"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话:" label-width="150px" prop="mobile">
          <el-input
            style="width: 85%"
            v-model="addForm.mobile"
            placeholder="请输入"
            maxlength="11"
            show-word-limit
            autocomplete="off"
          ></el-input>
        </el-form-item>
      <el-form-item label="负责区域:" label-width="150px" prop="regionId">
       <el-select
          style="width: 85%"
          v-model="addForm.regionId"
          @change="findName"
          placeholder="请选择"
        >
          <el-option
            v-for="item in regionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="头像:" label-width="150px" prop="image">
        <ImportImg v-model="addForm.image" />
      </el-form-item>
      <el-form-item label="状态:" label-width="150px">
        <el-checkbox v-model="addForm.status">是否启用</el-checkbox>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button
            class="btn cancel"
            @click="closeFn"
            style="margin-right: 20px"
            >取消</el-button
          >
          <el-button class="btn confirm" type="primary" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { getUserListApi, addRoleApi, editRoleApi } from "@/api/user";
import { searchRegionApi } from "@/api/node";
import ImportImg from '@/views/vm/components/importImg.vue'
export default {
  props: {
    isShow: Boolean,
    title: String,
    UserNow: Object
  },
  components:{
    ImportImg
  },
  data() {
    return {
      addForm: {
        image: '',
        mobile: "",
        regionId: "",
        regionName:'',
        roleId:'',
        status:'',
        userName:'',
      },
      userList:[],
      regionList:[],
      pageParams:{
        pageIndex:1,
        pageSize:1000
      },
      rules: {
        userName: [
          { required: true, message: "请输入人员名称", trigger: "blur" },
        ],
        roleId: [{ required: true, message: "请选择角色", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        regionId: [{ required: true, message: "请选择负责区域", trigger: "blur" }],
        image: [{ required: true, message: "请上传头像", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getUserList()
    this.getRegionList()
    if(this.title === '编辑人员'){
      this.addForm = this.UserNow
    }
  },
  methods: {
    async getUserList(){
      this.userList = await getUserListApi()
    },

    async getRegionList(){
      const res = await searchRegionApi(this.pageParams)
      this.regionList = res.currentPageRecords
    },
    
    findName(val){
      const res = this.regionList.find(item => item.id === val )
      this.addForm.regionName = res.name
    },

    closeFn() {
      this.$refs.addForm.resetFields();
      this.addForm = {
        image: '',
        mobile: "",
        regionId: "",
        regionName:'',
        roleId:'',
        status:'',
        userName:'',
      };
      this.$emit("update:isShow", false);
    },
    submit() {
      this.$refs.addForm.validate(async (pass) => {
        if (pass) {
          if(this.title === '新增人员'){
            await addRoleApi(this.addForm);
            this.$message.success("添加人员成功");
          } else {
            await editRoleApi(this.addForm)
            this.$message.success("编辑人员成功");
          }
            this.closeFn();
            this.$emit('searchUser')
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  width: 80px;
  height: 36px;
  border: none;
  padding: 0;
  line-height: 36px;
}
.cancel {
  background-color: #fbf4f0;
  color: #7e7570;
}
.confirm {
  background-color: #ff6d29;
  color: #fff;
}
</style>
